import { getMenuPageConfigInfo, getMenuVersion } from "@/api/global"
import IndexedDBCache from "@/utils/IndexedDB/src/IndexedDBCache"
export const setMenuConfigCache = (menuId) => {
    return new Promise((resolve, reject) => {
        getMenuVersion(menuId).then(version => {
            let db = new IndexedDBCache("Menu_Config", "menuId");
            db.init().then((request) => {
                if (request.code == 200) {
                    db.get(menuId).then(data => {
                        // 判断缓存是否存在
                        if (data) {
                            console.log(data)
                            // 判断缓存是否过期
                            if (data.menuVersionNo == version) {
                                // 缓存未过期, 关闭数据库
                                db.close();
                                resolve({
                                    code: 200,
                                    data
                                });
                            } else {
                                // 重新获取数据
                                getMenuPageConfigInfo(menuId).then(res => {
                                    db.update(menuId, {
                                        menuId: menuId,
                                        ...res
                                    }).finally(() => {
                                        resolve({
                                            code: 200,
                                            data: res
                                        })
                                    })
                                    db.close();
                                    resolve({
                                        code: 200,
                                        data: res
                                    });
                                })
                            }

                        } else {
                            getMenuPageConfigInfo(menuId).then(res => {
                                db.insert({
                                    menuId: menuId,
                                    ...res
                                }).finally(() => {
                                    resolve({
                                        code: 200,
                                        data: res
                                    })
                                })
                            })
                        }
                    })
                } else {
                    reject(request)
                }
            })
        }).catch(err => {
            reject(err)
        })
    })
}

export const clearMenuConfigCache = () => {
    let db = new IndexedDBCache("Menu_Config", "menuId");
    return db.clear()
}

export const getMenuConfigCache = (menuId) => {
    return new Promise((resolve, reject) => {
        let db = new IndexedDBCache("Menu_Config", "menuId");
        db.init().then((request) => {
            if (request.code == 200) {
                db.get(menuId).then(data => {
                    resolve({
                        code: 200,
                        data
                    })
                    db.close();
                })
            } else {
                reject({
                    code: 500,
                    data: null
                })
                db.close();
            }
        })
    })
}

